﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace odinshrine
{
    class Mainmenu:Menu
    {
                                        //3 Menus: Principal, Options, Commandes
        //Menu Principal
        static GestionPhysics phys = new GestionPhysics();
        static Texture2D jouer, quitter, options, fond_menu;
        public static Texture2D imgBouton;
        public static Rectangle jouerRect, quitterRect, optionsRect;
        public const int jouerX = 450, quitterX = 450, optionsX = 450;
        public static int jouerY = 200, quitterY = 400, optionsY = 300;
        public static int placeMainMenu = 0;
        
        //Etat du menu
        TimeSpan timerBouton = TimeSpan.Zero;
        public static void Gestion(MouseState mouse, Game1 game, GameTime gameTime, KeyboardState clavier)
        {
            gestionClavier(clavier, mouse, game);
        }

        //Gere le menu en Entier
        /*private static void GestionSouris(MouseState mouse, Game1 game, GameTime gameTime)
        {
            //If menu_on
            mouse = Mouse.GetState();
            if (timer_souris.Milliseconds > delaiSouris)
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    timer_souris = TimeSpan.Zero;

                    if (JouerSouris(mouse.X, mouse.Y))
                    {
                        menu_on = false;
                        placeMainMenu = 0;
                        timer_souris = TimeSpan.Zero;
                        placeMainMenu = 0;
                    }
                    if (OptionsSouris(mouse.X, mouse.Y))
                    {
                        menu_options = true;
                        quitterRect.X = -1000;
                        jouerRect.X = -1000;
                        optionsRect.X = -1000;
                        Options.menuRect.X = Options.menuX;
                        Options.menuRect.Y = Options.menuY;
                        Options.commandesRect.X = Options.commandesX;
                        Options.commandesRect.Y = Options.commandesY;
                        Options.sonRect.X = Options.sonX;
                        Options.sonRect.Y = Options.sonY;
                        placeMainMenu = 0;

                    }
                    if (QuitterSouris(mouse.X, mouse.Y))
                    {
                        game.Exit();
                    }
                }
            }

            timer_souris += gameTime.ElapsedGameTime;
        }*/

        private static void gestionClavier(KeyboardState clavier, MouseState mouse, Game1 game)
        {
            if (menu_on && !menu_options && !menu_commandes)
            {
                //Pour gerer le "timerbouton"
                if (clavier.IsKeyUp(Keys.Down))
                {
                    clavierUpdown = true;
                }
                if (clavier.IsKeyUp(Keys.Up))
                {
                    clavierUpup = true;
                }

                if (clavier.IsKeyDown(Keys.Down) && placeMainMenu != 3 && clavierUpdown)
                {
                    placeMainMenu++;
                    clavierUpdown = false;
                }
                if (clavier.IsKeyDown(Keys.Up) && placeMainMenu != 1 && clavierUpup)
                {
                    placeMainMenu--;
                    clavierUpup = false;
                }
                
            }
            if (placeMainMenu == 1 && clavier.IsKeyDown(Keys.Enter))
            {
                menu_on = false;
                placeMainMenu = 0;
                timer_souris = TimeSpan.Zero;
            }
            if (placeMainMenu == 2 && clavier.IsKeyDown(Keys.Enter))
            {
                menu_options = true;
                quitterRect.X = -1000;
                jouerRect.X = -1000;
                optionsRect.X = -1000;
                Options.menuRect.X = Options.menuX;
                Options.menuRect.Y = Options.menuY;
                Options.commandesRect.X = Options.commandesX;
                Options.commandesRect.Y = Options.commandesY;
                Options.sonRect.X = Options.sonX;
                Options.sonRect.Y = Options.sonY;
                placeMainMenu = 0;
            }

            if (placeMainMenu == 3 && clavier.IsKeyDown(Keys.Enter))
            {
                game.Exit();
            }
        }

        /*private static bool sourismainMenu(int x, int y)
        {
            return (QuitterSouris(x, y) || JouerSouris(x, y) || OptionsSouris(x, y)); 
        }*/

        //Initialise le menu princial
        public static void initmainMenu(ContentManager Content)
        {
            jouer = Content.Load<Texture2D>("Menu/jouer");
            quitter = Content.Load<Texture2D>("Menu/quitter");
            options = Content.Load<Texture2D>("Menu/options");
            fond_menu = Content.Load<Texture2D>("Menu/menu");
            imgBouton = Content.Load<Texture2D>("Menu/imgBouton");

            jouerRect = new Rectangle(jouerX, jouerY, jouer.Width, jouer.Height);
            optionsRect = new Rectangle(optionsX, optionsY, options.Width, options.Height);
            quitterRect = new Rectangle(quitterX, quitterY, quitter.Width, quitter.Height);
        }

        //Dessine le menu principal
        public static void drawmainMenu(SpriteBatch spriteBatch, int mouseX, int mouseY)
        {
            spriteBatch.Draw(fond_menu, new Vector2(0, 0), Color.White);
            spriteBatch.Draw(jouer, new Vector2(jouerX, jouerY), Color.White);
            spriteBatch.Draw(options, new Vector2(optionsX, optionsY), Color.White);
            spriteBatch.Draw(quitter, new Vector2(quitterX, quitterY), Color.White);
            //Clavier
            if (placeMainMenu == 1)
            {
                spriteBatch.Draw(imgBouton, new Vector2(jouerX - 70, jouerY), Color.White);
            }
            if (placeMainMenu == 2)
            {
                spriteBatch.Draw(imgBouton, new Vector2(optionsX - 70, optionsY), Color.White);
            }
            if (placeMainMenu == 3)
            {
                spriteBatch.Draw(imgBouton, new Vector2(quitterX - 70, quitterY), Color.White);
            }
        }
    }
}
